/************************************  


DESCRIPTION: Get the specialty, state, and zip code of the physicians seen by beneficiaries

Datasets Used:
 (1) ANALYSIS_V3_`i'
  
Datasets Created:
	(1) DR_CHARS_QTR_2005_2012
	(2) DR_BENE_CHARS_QTR_2005_2012
		
************************************/

set more off
capture log close
clear all

global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\"
global datadoc "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global propscore "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2


log using ".\2a.DocChars.log", replace

*PHYSICIAN CHARACTERISTICS  
* These files are not bene-level files. They are bene-docid files
use NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID using "$dataOut/ANALYSIS_V3_2005.dta"  
 append using "$dataOut/ANALYSIS_V3_2006.dta", keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID )  
 append using "$dataOut/ANALYSIS_V3_2007.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID )
 append using "$dataOut/ANALYSIS_V3_2008.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID )
 append using "$dataOut/ANALYSIS_V3_2009.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID )
 append using "$dataOut/ANALYSIS_V3_2010.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID )
 append using "$dataOut/ANALYSIS_V3_2011.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID )
 append using "$dataOut/ANALYSIS_V3_2012.dta"  , keep(NPI UPIN TAX_NUM1 PHYS* quarter npi_ID upin_ID )
	gen docid=NPI
	replace docid=UPIN if NPI=="" & docid==""
	
	drop if docid==""
	drop NPI UPIN npi upin
	duplicates drop

	sort docid quarter
		by docid: replace TAX=TAX[_n-1] if TAX=="" & _n~=1
		by docid: replace TAX=TAX[_n+1] if TAX=="" & _n~=_N
		by docid: replace TAX=TAX[_N] if TAX==""

	*Ensure that specialty and state are nonmissing on the dataset for each doctor	
	foreach var of varlist PHYS_SPCLTY PHYS_STATE PHYS_ZIP {
		sort docid quarter `var'
			by docid quarter: replace `var'=`var'[_N] if `var'==""
		sort docid `var'
			by docid : replace `var'=`var'[_N] if `var'==""
	}


*Physician ZIP code and state
	foreach var of varlist PHYS_STATE PHYS_ZIP {
		sort TAX quarter `var'
		 by TAX quarter: replace `var'=`var'[_N] if `var'=="" & `var'[1]==`var'[_N]
		sort TAX `var'
	by TAX : replace `var'=`var'[_N] if `var'=="" & `var'[1]==`var'[_N]
	}

*Firm size
duplicates drop
bysort TAX_NUM1 quarter: gen firmsize=_N

	gen fs5=firmsize<=5
	gen fs5_25=firmsize>5 & firmsize<=25
	gen fs25_50=firmsize>25 & firmsize<=50
	gen fs50_100=firmsize>50 & firmsize<=100
	gen fs100_200=firmsize>100 & firmsize<=200
	gen fsgt200=firmsize>200  

replace firmsize=. if TAX==""
foreach var of varlist fs* {
replace `var'=. if TAX==""
}
 
	duplicates drop
	duplicates tag docid quarter, gen(tag)
	 drop if TAX=="" & tag>0
	 drop if firmsize==. & tag>0
	 drop if tag>5
	duplicates drop
	 drop tag

sort docid quarter
by docid quarter: gen id=_n
reshape wide PHYS_STATE PHYS_ZIP PHYS_SPCLTY TAX, i(docid quarter firmsize fs* ) j(id)
duplicates tag docid quarter, gen(tag)
drop if TAX_NUM1=="" if tag>0
drop if tag>0 & PHYS_STATE1==""
drop if firmsize==.

sort docid quarter
save "$datadoc\DR_CHARS_QTR_2005_2012.dta", replace

use BENE_ID NPI UPIN quarter using "$dataOut/ANALYSIS_V3_2005.dta"  
 append using "$dataOut/ANALYSIS_V3_2006.dta", keep(BENE_ID NPI UPIN quarter )  
 append using "$dataOut/ANALYSIS_V3_2007.dta"  , keep(BENE_ID NPI UPIN quarter )
 append using "$dataOut/ANALYSIS_V3_2008.dta"  , keep(BENE_ID NPI UPIN quarter )
 append using "$dataOut/ANALYSIS_V3_2009.dta"  , keep(BENE_ID NPI UPIN quarter )
 append using "$dataOut/ANALYSIS_V3_2010.dta"  , keep(BENE_ID NPI UPIN quarter )
 append using "$dataOut/ANALYSIS_V3_2011.dta"  , keep(BENE_ID NPI UPIN quarter  )
 append using "$dataOut/ANALYSIS_V3_2012.dta"  , keep(BENE_ID NPI UPIN quarter )
  gen docid=NPI
 replace docid=UPIN if NPI=="" & docid==""
drop if docid==""
 
sort docid quarter
merge m:1 docid quarter using "$datadoc\DR_CHARS_QTR_2005_2012.dta"

gen id=1

collapse (count) doc_Cnt=id (min) mnfirmsize=firmsize (max) mxfirmsize=firmsize fs*, by(BENE_ID quarter)

save "$datadoc\DR_BENE_CHARS_QTR_2005_2012.dta", replace




